home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / comms / non-internet / samba / source / status.c < prev    next >
C/C++ Source or Header  |  1996-06-26  |  7KB  |  259 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    status reporting
  5.    Copyright (C) Andrew Tridgell 1994-1995
  6.    
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.    
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. /*
  23.  * This program reports current SMB connections
  24.  */
  25.  
  26. #ifdef SYSLOG
  27. #undef SYSLOG
  28. #endif
  29.  
  30. #include "includes.h"
  31. #include "loadparm.h"
  32.  
  33. struct connect_record crec;
  34. extern int DEBUGLEVEL;
  35. extern FILE *dbf;
  36.  
  37. static pstring Ucrit_username = "";                   /* added by OH */
  38. int            Ucrit_pid[100];  /* Ugly !!! */        /* added by OH */
  39. int            Ucrit_MaxPid=0;                        /* added by OH */
  40. unsigned int   Ucrit_IsActive = 0;                    /* added by OH */
  41. void           Ucrit_addUsername(pstring username);   /* added by OH */
  42. unsigned int   Ucrit_checkUsername(pstring username); /* added by OH */
  43. void           Ucrit_addPid(int pid);                 /* added by OH */
  44. unsigned int   Ucrit_checkPid(int pid);               /* added by OH */
  45.  
  46. int main(int argc, char *argv[])
  47. {
  48.   FILE *f;
  49.   pstring fname;
  50.   int uid, c, n;
  51.   static pstring servicesf = CONFIGFILE;
  52.   extern char *optarg;
  53.   int verbose = 0;
  54.   void *dir;
  55.   char *s;
  56.   BOOL firstopen=True;
  57.   BOOL processes_only=False;
  58.   int last_pid=0;
  59.  
  60.   setup_logging(argv[0],True);
  61.  
  62.   charset_initialise();
  63.  
  64.   DEBUGLEVEL = 0;
  65.   dbf = fopen("/dev/null","w");
  66.  
  67.   if (getuid() != geteuid()) {
  68.     printf("smbstatus should not be run setuid\n");
  69.     return(1);
  70.   }
  71.  
  72.   while ((c = getopt(argc, argv, "pdsu:")) != EOF) {
  73.     switch (c) {
  74.     case 'd':
  75.       verbose = 1;
  76.       break;
  77.     case 'p':
  78.       processes_only = 1;
  79.       break;
  80.     case 's':
  81.       strcpy(servicesf, optarg);
  82.       break;
  83.     case 'u':                                       /* added by OH */
  84.       Ucrit_addUsername(optarg);                    /* added by OH */
  85.       break;
  86.     default:
  87.       fprintf(stderr, "Usage: %s [-d] [-p] [-s configfile] [-u username]\n", *argv); /* changed by OH */
  88.       return (-1);
  89.     }
  90.   }
  91.  
  92.  
  93.  
  94.   if (!lp_load(servicesf,False)) {
  95.     fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
  96.     return (-1);
  97.   }
  98.  
  99.   if (verbose) {
  100.     printf("using configfile = %s\n", servicesf);
  101.     printf("lockdir = %s\n", *lp_lockdir() ? lp_lockdir() : "NULL");
  102.   }
  103.  
  104.   strcpy(fname,lp_lockdir());
  105.   standard_sub_basic(fname);
  106.   trim_string(fname,"","/");
  107.   strcat(fname,"/STATUS..LCK");
  108.  
  109.   f = fopen(fname,"r");
  110.   if (!f) {
  111.     printf("Couldn't open status file %s\n",fname);
  112.     if (!lp_status(-1))
  113.       printf("You need to have status=yes in your smb config file\n");
  114.     return(0);
  115.   }
  116.  
  117.   uid = getuid();
  118.  
  119.   if (!processes_only) {
  120.     printf("\nSamba version %s\n",VERSION);
  121.  
  122.     printf("Service      uid      gid      pid     machine\n");
  123.     printf("----------------------------------------------\n");
  124.   }
  125.  
  126.   while (!feof(f))
  127.     {
  128.       if (fread(&crec,sizeof(crec),1,f) != 1)
  129.     break;
  130.       if ( crec.magic == 0x280267 && process_exists(crec.pid) 
  131.            && Ucrit_checkUsername(uidtoname(crec.uid))                      /* added by OH */
  132.          )
  133.       {
  134.         Ucrit_addPid(crec.pid);                                             /* added by OH */
  135.     if (processes_only) {
  136.       if (last_pid != crec.pid)
  137.         printf("%d\n",crec.pid);
  138.       last_pid = crec.pid; /* XXXX we can still get repeats, have to
  139.                   add a sort at some time */
  140.     }
  141.     else      
  142.       printf("%-10.10s   %-8s %-8s %5d   %-8s (%s) %s",
  143.          crec.name,uidtoname(crec.uid),gidtoname(crec.gid),crec.pid,
  144.          crec.machine,crec.addr,
  145.          asctime(LocalTime(&crec.start,GMT_TO_LOCAL)));
  146.       }
  147.     }
  148.   fclose(f);
  149.  
  150.   if (processes_only) exit(0);
  151.  
  152.   printf("\n");
  153.  
  154.   dir = opendir(lp_lockdir());
  155.   if (!dir) return(0);
  156.   while ((s=readdirname(dir))) {
  157.     char buf[16];
  158.     int pid,mode;
  159.     time_t t;
  160.     int fd;
  161.     pstring lname;
  162.     int dev,inode;
  163.  
  164.     if (sscanf(s,"share.%d.%d",&dev,&inode)!=2) continue;
  165.  
  166.     strcpy(lname,lp_lockdir());
  167.     trim_string(lname,NULL,"/");
  168.     strcat(lname,"/");
  169.     strcat(lname,s);
  170.  
  171.     fd = open(lname,O_RDONLY,0);
  172.     if (fd < 0) continue;
  173.     if (read(fd,buf,16) != 16) continue;
  174.     n = read(fd,fname,sizeof(fname));
  175.     fname[MAX(n,0)]=0;
  176.     close(fd);
  177.  
  178.     t = IVAL(buf,0);
  179.     mode = IVAL(buf,4);
  180.     pid = IVAL(buf,8);
  181.  
  182.     if ( !Ucrit_checkPid(pid) )             /* added by OH */
  183.         continue;
  184.  
  185.     if (IVAL(buf,12) != LOCKING_VERSION || !process_exists(pid)) {
  186.       if (unlink(lname)==0)
  187.         printf("Deleted stale share file %s\n",s);
  188.       continue;
  189.     }
  190.  
  191.     fname[sizeof(fname)-1] = 0;
  192.  
  193.     if (firstopen) {
  194.       firstopen=False;
  195.       printf("Locked files:\n");
  196.       printf("Pid    DenyMode   R/W     Name\n");
  197.       printf("------------------------------\n");
  198.     }
  199.  
  200.  
  201.     printf("%-5d  ",pid);
  202.     switch ((mode>>4)&0xF)
  203.       {
  204.       case DENY_NONE: printf("DENY_NONE  "); break;
  205.       case DENY_ALL:  printf("DENY_ALL   "); break;
  206.       case DENY_DOS:  printf("DENY_DOS   "); break;
  207.       case DENY_READ: printf("DENY_READ  "); break;
  208.       case DENY_WRITE:printf("DENY_WRITE "); break;
  209.       }
  210.     switch (mode&0xF) 
  211.       {
  212.       case 0: printf("RDONLY "); break;
  213.       case 1: printf("WRONLY "); break;
  214.       case 2: printf("RDWR   "); break;
  215.       }
  216.     printf(" %s   %s",fname,asctime(LocalTime(&t,GMT_TO_LOCAL)));
  217.   }
  218.   closedir(dir);
  219.  
  220.   if (firstopen)
  221.     printf("No locked files\n");
  222.  
  223.   return (0);
  224. }
  225.  
  226. /* added by OH */
  227. void Ucrit_addUsername(pstring username)
  228. {
  229.   strcpy(Ucrit_username, username);
  230.   if(strlen(Ucrit_username) > 0)
  231.     Ucrit_IsActive = 1;
  232. }
  233.  
  234. unsigned int Ucrit_checkUsername(pstring username)
  235. {
  236.   if ( !Ucrit_IsActive) return 1;
  237.   if (strcmp(Ucrit_username,username) ==0) return 1;
  238.   return 0;
  239. }
  240.  
  241. void Ucrit_addPid(int pid)
  242. {
  243.   int i;
  244.   if ( !Ucrit_IsActive) return;
  245.   for (i=0;i<Ucrit_MaxPid;i++)
  246.     if( pid == Ucrit_pid[i] ) return;
  247.   Ucrit_pid[Ucrit_MaxPid++] = pid;
  248. }
  249.  
  250. unsigned int   Ucrit_checkPid(int pid)
  251. {
  252.   int i;
  253.   if ( !Ucrit_IsActive) return 1;
  254.   for (i=0;i<Ucrit_MaxPid;i++)
  255.     if( pid == Ucrit_pid[i] ) return 1;
  256.   return 0;
  257. }
  258.  
  259.